class Variables: # A container for variables that need to be passed between functions.
MeshArray = list()
VertArray = list()
UVArray = list()
UVPadding = 0
FaceVerts = 0
FaceBytes = 0
FacePadding = 0
FaceCount = 0
VertCount = 0
Verts = 0
HeaderPadding = 0
def Header_Parser():
file.seek(-7,1) # Jump back to important header bytes.
byte = file.read(1) # This byte identifies the mesh type.
if byte == b'\x12': # 'Loose' byte triangles.
vars.FaceVerts = 3
vars.FaceBytes = 1
vars.FacePadding = 3
vars.HeaderPadding = 6
vars.UVPadding = 6
elif byte == b'\x14': # Byte faces.
vars.FaceVerts = 4
vars.FaceBytes = 1
vars.FacePadding = 4
vars.HeaderPadding = 8
vars.UVPadding = 8
elif byte == b'\x11': # 'Loose' short triangles.
vars.FaceVerts = 3
vars.FaceBytes = 2
vars.FacePadding = 6
vars.HeaderPadding = 8
vars.UVPadding = 8
elif byte == b'\x13': # Short faces.
vars.FaceVerts = 4
vars.FaceBytes = 2
vars.FacePadding = 8
vars.HeaderPadding = 8
vars.UVPadding = 8
vars.FaceCount = int.from_bytes(file.read(2), 'little') # Get number of faces following header.
file.seek(4 + vars.HeaderPadding,1) # Skip rest of header plus padding.
def Face_Parser():
CurrentMesh = len(vars.MeshArray) - 1 # Get the newest entry in the mesh array.
vars.MeshArray[CurrentMesh].append([0,0,0]) if vars.FaceVerts == 3 else vars.MeshArray[CurrentMesh].append([0,0,0,0]) # Create a new entry within it.
CurrentFaceIndex = len(vars.MeshArray[CurrentMesh]) - 1 # Get this new entry.
for x in range(vars.FaceVerts):
vars.MeshArray[CurrentMesh][CurrentFaceIndex][x] = int.from_bytes(file.read(vars.FaceBytes), 'little') # Write verts to the current face index.
if vars.MeshArray[CurrentMesh][CurrentFaceIndex][x] >= vars.VertCount: vars.VertCount = vars.MeshArray[CurrentMesh][CurrentFaceIndex][x] + 1 # Get total number of verts from largest face vertex. Must add one for accurate count.
file.seek(vars.FacePadding, 1) # Skip padding between the face and its UVs.
def UV_Parser():
CurrentMesh = len(vars.UVArray) - 1 # Get the newest entry in the UV array.
for x in range(vars.FaceVerts): # Every vert has a UV.
vars.UVArray[CurrentMesh].append([0,0]) # Create a new entry within it.
CurrentUVIndex = len(vars.UVArray[CurrentMesh]) - 1 # Get this new entry.
# Get and store the UV coordinates.
vars.UVArray[CurrentMesh][CurrentUVIndex][0] = int.from_bytes(file.read(2), "little", signed = "True") / 4096